﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Caifu.Clients;
using Itfort.Binder;

namespace Caifu.WebAdmin.Funds
{
	public partial class EditFund : System.Web.UI.Page
	{
		// 0：添加前，1：添加后，2：出错时
		protected int m_OpState = 0;

		protected string m_Code;
		protected Fund m_Fund;

		protected void Page_Load(object sender, EventArgs e)
		{
			m_Code = Itfort.Web.RequestUtil.ToString("code");
			m_Fund = FundClient.GetFund(m_Code);
			if (!this.IsPostBack)
			{
				this.InitControls();

				BindUtil.Bind2Page(m_Fund, this, "f_");
			}
		}

		protected void BtnOK_Click(object sender, EventArgs e)
		{
			ConverterFactory.ConvertTo<Fund>(Request.Form, "f_", m_Fund);
			m_Fund.IsQDII = this.f_IsQDII.Checked;
			FundClient.UpdateFund(m_Fund);
			m_OpState = 1;
			this.InitControls();
			BindUtil.Bind2Page(m_Fund, this, "f_");
		}

		protected void InitControls()
		{
			BindUtil.Bind2DropDownList(
				ConstantDefinition.GetFundTypes().ToDictionary(kv => (object)kv.Key, kv => (object)kv.Value)
				, this.f_FundType);

			BindUtil.Bind2DropDownList(
				ConstantDefinition.GetInvestmentTypes().ToDictionary(kv => (object)kv.Key, kv => (object)kv.Value)
				, this.f_InvestmentType);

			BindUtil.Bind2DropDownList(
				ConstantDefinition.GetInvestmentStyles().ToDictionary(kv => (object)kv.Key, kv => (object)kv.Value)
				, this.f_InvestmentStyle);

			BindUtil.Bind2DropDownList(
				ConstantDefinition.GetFundStates().ToDictionary(kv => (object)kv.Key, kv => (object)kv.Value)
				, this.f_FundState);

			var investAdvisors = InvestAdvisorClient.GetAllInvestAdvisors();
			BindUtil.Bind2DropDownList(
				investAdvisors.ToDictionary(p => (object)p.Id, p => (object)p.Name)
				, this.f_InvestAdvisorId);

			var trustees = TrusteeClient.GetTrustees();
			BindUtil.Bind2DropDownList(
				trustees.ToDictionary(p => (object)p.Id, p => (object)p.Name)
				, this.f_TrusteeId);
		}
	}
}